package login;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import create.CreateUser;
import stuMaiFrame.StuMainFrame;
import teaMainFrame.TeaMainFrame;
import util.MD5;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Window;
public class login extends JFrame {
	private int username;
	private String password;
	private int flag;
	private JPanel contentPane;
	private JTextField usertextField;
	private JTextField pwdtextField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {
		setVisible(true);
		setBackground(Color.LIGHT_GRAY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(700, 400, 450, 300);
		setLocationRelativeTo(getOwner());
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("学生学籍管理系统");
		label.setBounds(164, 6, 112, 50);
		contentPane.add(label);
		
		usertextField = new JTextField();
		usertextField.setBounds(142, 77, 130, 26);
		contentPane.add(usertextField);
		usertextField.setColumns(10);
		
		
		
		JLabel label_1 = new JLabel("账号");
		label_1.setBounds(104, 82, 26, 16);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("密码");
		label_2.setBounds(104, 110, 26, 16);
		contentPane.add(label_2);
		
		pwdtextField = new JPasswordField();
		pwdtextField.setBounds(142, 105, 130, 26);
		contentPane.add(pwdtextField);
		pwdtextField.setColumns(10);
		password = pwdtextField.getText();
		
		usertextField.addKeyListener(new KeyAdapter(){  
            public void keyTyped(KeyEvent e) {  
                int keyChar = e.getKeyChar();                 
                if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){  
                }else{  
                    e.consume();   
                }  
            }  
        }); 
		
		
		JButton btnNewButton = new JButton("登录");
		try{
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println(e.getActionCommand());
				String  s = usertextField.getText();
				username = Integer.parseInt(s);// bug
				String ps = pwdtextField.getText();//加密之前的密码
				password = MD5.getMD5(ps); 
				if(String.valueOf(username).equals("")&&ps.equals("")){
					JOptionPane.showMessageDialog(null,"用户名和密码为空","信息提示框",JOptionPane.INFORMATION_MESSAGE);
					}
					else if(!String.valueOf(username).equals("")&&ps.equals("")){
						JOptionPane.showMessageDialog(null,"密码为空","信息提示框",JOptionPane.INFORMATION_MESSAGE);
						}
					else if(String.valueOf(username).equals("")&&!ps.equals("")){
						JOptionPane.showMessageDialog(null,"用户名为空","信息提示框",JOptionPane.INFORMATION_MESSAGE);
						}
					else{
						 Check b = new Check(username,password);
						if(flag == 1){
							if (b.CheckStu() == false){
								JOptionPane.showMessageDialog(null,"用户名或密码错误","信息提示框",JOptionPane.INFORMATION_MESSAGE);
							}else{
								JOptionPane.showMessageDialog(null,"登陆成功！","信息提示框",JOptionPane.INFORMATION_MESSAGE);
								new StuMainFrame(username);
								setVisible (false);
							}
						}else if (flag == 2)
							if (b.CheckTea() == false){
								JOptionPane.showMessageDialog(null,"用户名或密码错误","信息提示框",JOptionPane.INFORMATION_MESSAGE);
							}else{
								JOptionPane.showMessageDialog(null,"登陆成功","信息提示框",JOptionPane.INFORMATION_MESSAGE);

								new TeaMainFrame(username);
								setVisible (false);
							}else{
								JOptionPane.showMessageDialog(null,"请填写身份信息","信息提示框",JOptionPane.INFORMATION_MESSAGE);
							}
					}
				
				
			}
		});
		}catch(Exception e){
			JOptionPane.showMessageDialog(null,"输入错误","信息提示框",JOptionPane.INFORMATION_MESSAGE);
		}
		btnNewButton.setBounds(142, 152, 78, 29);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("新建用户");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CreateUser c = new CreateUser();
				setVisible(false);
			}
		});
		btnNewButton_1.setBounds(236, 152, 78, 29);
		contentPane.add(btnNewButton_1);
		
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("老师用户");
		rdbtnNewRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flag = 2;
			}
		});
		rdbtnNewRadioButton.setBounds(286, 78, 141, 23);
		contentPane.add(rdbtnNewRadioButton);
		
		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("学生用户");
		rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flag = 1;
			}
		});
		rdbtnNewRadioButton_1.setBounds(286, 110, 141, 23);
		contentPane.add(rdbtnNewRadioButton_1);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(rdbtnNewRadioButton);
		bg.add(rdbtnNewRadioButton_1);
	}
}
